package com.bstech.Utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.util.Log;

public class Files {

	/* GESTIONE FILES */
	public static boolean CreateDir(String dir) {
		File tempdir = new File(dir);
		if (!tempdir.exists())
			tempdir.mkdirs();
		return (tempdir.isDirectory());
	}

	public static void DeleteDirectory(File fileOrDirectory) {
		if (fileOrDirectory.isDirectory())
			for (File child : fileOrDirectory.listFiles())
				DeleteDirectory(child);
		fileOrDirectory.delete();
	}

	public static byte[] ReadFile(String filepath) {
		File fcheck = new File(filepath);
		if (fcheck.exists()) {

			InputStream instr = null;

			try {
				instr = new BufferedInputStream(new FileInputStream(fcheck));
			} catch (FileNotFoundException e) {
				return null;
			}

			int buffsize = 512;
			byte[] buff = new byte[buffsize];

			int offs = 0;
			int read = 1;
			int toread = 0;

			try {
				while (read > 0) {
					toread = instr.available();
					if (toread == 0)
						break;
					if (toread + offs > buffsize)
						buff = (byte[]) resizeArray(buff, toread + offs);

					read = instr.read(buff, offs, toread);
					offs += read;
				}
				instr.close();
				buff = (byte[]) resizeArray(buff, read);
				return buff;
			} catch (IOException ex) {
				Log.e("ReadFile", "error in read cycle", ex);
				return null;
			}
		} else
			return null;
	}

	public static byte[] ReadRemoteFile(String remoteFilePath) {
		try {
			 URL url = new URL(remoteFilePath);
             URLConnection ucon = url.openConnection();

             InputStream is = ucon.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(is);

             ByteArrayBuffer baf = new ByteArrayBuffer(50);
             int current = 0;
             while ((current = bis.read()) != -1) {
                     baf.append((byte) current);
             }
             
             byte[] ret = baf.toByteArray();
             
             baf = null;
             bis.close();
             is.close();
             ucon = null;
             url = null;
             
             return ret;
		} catch (MalformedURLException e) {
			return null;
		} catch (IOException e) {
			return null;
		}
	}

	public static boolean WriteFile(byte[] buff, String filepath) {
		boolean ret = false;

		File fcheck = new File(filepath);
		if (fcheck.exists())
			fcheck.delete();
		try {
			if (!fcheck.createNewFile())
				return false;

			OutputStream outstr = null;
			try {
				outstr = new BufferedOutputStream(new FileOutputStream(fcheck));
				try {
					outstr.write(buff);
					outstr.flush();
					outstr.close();
					ret = true;
				} catch (IOException e) {
					Log.e("WriteFile",
							"operations while writing file thrown an exception",
							e);
				}
			} catch (FileNotFoundException e) {
				Log.e("WriteFile",
						"new BufferedOutputStream(...) thrown an exception", e);
			}

		} catch (IOException e) {
			Log.e("WriteFile", "createNewFile thrown an exception", e);
			return false;
		}

		return ret;
	}

	public static Object resizeArray(Object oldArray, int newSize) {
		int oldSize = java.lang.reflect.Array.getLength(oldArray);
		Class<?> elementType = oldArray.getClass().getComponentType();
		Object newArray = java.lang.reflect.Array.newInstance(elementType,
				newSize);
		int preserveLength = Math.min(oldSize, newSize);
		if (preserveLength > 0)
			System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
		return newArray;
	}
}
